/*
题目链接：https://leetcode.cn/problems/house-robber-iii/description/
	刘沛民	2024-12-3
*/

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  
    vector<int> travel(TreeNode* root)
    {
        if(root == nullptr)
        {
            return {0,0} ;
        }  
        
        vector<int> vals = {0,0}; //vals[0] 当前最大值，vals[1] 不打劫当前结点的最大值
        vector<int> left = travel(root->left);
        vector<int> right = travel(root->right);
    
        //打劫该节点
        vals[0] = max(left[1]+right[1]+root->val,left[0]+right[0]);

        //不打劫该节点
        vals[1] = left[0]+right[0];
        
        return vals;
        
    }
    int rob(TreeNode* root) {
        vector<int> val = travel(root);
        return max(val[0],val[1]);
    }
};